package com.lime49.lockcrypt;
import com.lime49.lockcrypt.ui.AbstractDialog;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

/**
* A form used for editing account fields
*/
public class FrmAddGroup extends AbstractDialog {
    private ResourceBundle lang;
    private Group group;
    private JPanel content;
    private JTextField txtGroupName;
    private JComboBox cGroupParent;
    private boolean editing;
    
    private ArrayList<Group> groups;
    private AccountTree accountTree;
    
    public FrmAddGroup(FrmMainWindow owner, Group group, ArrayList<Group> groups, boolean editing, ResourceBundle lang, AccountTree accountTree) {
        super(owner, true);
        this.lang = lang;
        this.group = (Group)group.clone();
        this.groups = groups;
        this.accountTree = accountTree;
        if(editing) {
            this.editing = true;
            setTitle(lang.getString("EDITGROUP"));
        } else {
            this.editing = false;
            setTitle(lang.getString("ADDGROUP"));
        }
        KeyStroke escapeKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);
        Action escapeAction = new AbstractAction() {
            // close the frame when the user presses escape
            public void actionPerformed(ActionEvent e) {
                result = JOptionPane.CANCEL_OPTION;
                dispose();
            }
        }; 
        this.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escapeKeyStroke, "ESCAPE");
        this.getRootPane().getActionMap().put("ESCAPE", escapeAction);
        content = new JPanel(new GridBagLayout());
        content.setBorder(Constants.EMPTYBORDER);
        this.setContentPane(content);
        GridBagConstraints c = new GridBagConstraints();
        c.insets = Constants.GLOBALINSETS;
        c.weighty = 0.5;
        
        JLabel lblGroupName = new JLabel(lang.getString("GROUPNAME"));
            Utils.setgrid(c,0,0,1,1,GridBagConstraints.NONE,GridBagConstraints.WEST,0.5);
            content.add(lblGroupName,c);
        txtGroupName = new JTextField(20);
            lblGroupName.setLabelFor(txtGroupName);
            txtGroupName.setText(group.getName());
            Utils.setgrid(c,0,1,1,1,GridBagConstraints.REMAINDER,GridBagConstraints.CENTER,0.8);
            content.add(txtGroupName,c);
        
        JLabel lblParent = new JLabel(lang.getString("PARENTGROUP"));
            Utils.setgrid(c,1,0,1,1,GridBagConstraints.NONE,GridBagConstraints.WEST,0.5);
            content.add(lblParent,c);
        cGroupParent = new JComboBox();
            lblParent.setLabelFor(cGroupParent);
            cGroupParent.addItem("LockCrypt"); //the root node
            for(Group parentGroup : groups) {
                if(!parentGroup.getIsDeleted()) {
                    cGroupParent.addItem(parentGroup.getName());
                }
            }
            cGroupParent.setSelectedItem(group.getParentName());
            Utils.setgrid(c,1,1,1,1,GridBagConstraints.HORIZONTAL,GridBagConstraints.WEST,0.8);
            content.add(cGroupParent,c);
            
        JPanel buttonbox = new JPanel(new FlowLayout(FlowLayout.TRAILING));
            JButton btnAddGrp = new JButton(lang.getString("ADD"));
                btnAddGrp.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        if(validateGroup()) {
                            result = JOptionPane.OK_OPTION;
                            dispose();
                        }
                    }
                });
                this.getRootPane().setDefaultButton(btnAddGrp);
            if(!group.getName().equals("")) {
                btnAddGrp.setText(lang.getString("SAVE"));
                btnAddGrp.setActionCommand("edit");
            }
            JButton btncancel = new JButton(lang.getString("CANCEL"));
            btncancel.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent e) {
                   dispose();
               }
            });
            buttonbox.add(btnAddGrp);
            buttonbox.add(btncancel);
            btnAddGrp.setMinimumSize(Constants.MINSIZE);
            btnAddGrp.setPreferredSize(Constants.PREFSIZE);
            btnAddGrp.setMaximumSize(Constants.MAXSIZE);
            btncancel.setMinimumSize(Constants.MINSIZE);
            btncancel.setPreferredSize(Constants.PREFSIZE);
            btncancel.setMaximumSize(Constants.MAXSIZE);
            Utils.setgrid(c,2,0,2,1,GridBagConstraints.HORIZONTAL,GridBagConstraints.WEST,1);
            c.insets = new Insets(0,0,0,0);
            content.add(buttonbox,c);
        //this.getRootPane().setDefaultButton(btnAddGrp);
        //this.setMinimumSize(new Dimension(350,150));
        
        this.pack();
        this.setLocationRelativeTo(owner);
        this.setVisible(true);
    }
    
    /**
    * Gets the name of the group being edited
    * @return The name of the group being edited
    */
    public String getGroupName() {
        return txtGroupName.getText().trim();
    }
    
    /**
    * Gets the name of the group's parent
    * @return The name of the group's parent
    */
    public String getParentName() {
        return (String)cGroupParent.getSelectedItem();
    }
    
    /**
    * Validates that the group name is not blank and is unique
    * @return True if the group is valid, otherwise false
    */
    private boolean validateGroup() {
        String newName = txtGroupName.getText().trim();
        if(newName.length() == 0) {
            JOptionPane.showMessageDialog(this, lang.getString("ERR_GROUPNAMEBLANK"),lang.getString("ERR_GROUPNAMEINVALID"), JOptionPane.ERROR_MESSAGE);
            return false;
        } else if(!editing && Utils.getTreeIndex(newName,accountTree)>=0) {
            // Group exists in the tree, but we're editing the group (renaming)
            JOptionPane.showMessageDialog(this,lang.getString("ERR_GROUPEXISTS"),lang.getString("ERR_GROUPNAMEINVALID"), JOptionPane.ERROR_MESSAGE);
            return false;
        }
        return true;
    }
}
